<?php

require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/const.php' );
require_once( '../../libs/new_smt_variables.php' );
//1作成中  2 承認依頼中    3承認済み   4 承認却下    5客先OK  6客先NG

if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

$staff_id = $_SESSION['ADMIN_LOGIN']['STAFF_ID'];



if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	
	$mod = $_GET['mod'];
	
	if($mod=='new'){
		$db = new Class_DB;
		$date = date('Ymd');
		
		$strSql = "SELECT * FROM p_consumer_head WHERE  price_no like '%".$date."%' order by price_no desc limit 0,1";
		$strSql1 = "SELECT * FROM p_consumer where consumer_id=".$_GET['consumer_id'];
		$strSql2 = "SELECT * FROM M_STAFF WHERE staff_id=".$staff_id;
			
		$result = $db->query($strSql);
		$result1 = $db->query($strSql1);
		$result2 = $db->query($strSql2);
			
		$row = mysql_fetch_array($result);
		$row1 = mysql_fetch_array($result1);
		$row2 = mysql_fetch_array($result2);
			
	
		if(!empty($row['price_no'])){
			$strPrice_no = substr($row['price_no'],strlen($row['price_no'])-3,strlen($row['price_no']))+1;		
			if(strlen($strPrice_no) == 1){
				$row['price_no'] = $date."00".$strPrice_no;
			}elseif (strlen($strPrice_no) == 2){
				$row['price_no'] = $date."0".$strPrice_no;
			}else{
				$row['price_no'] = $date.$strPrice_no;
			}
					
		}else{
			$row['price_no'] = $date."001";				
		}							
				
		$price_no = $row['price_no'];
		$staff_name = $row2['staff_name'];
		
		$forms['remark']="「参考価格」は弊社オンラインショップでの販売価格を記載しています。";
		$forms['save_address']="貴社指定場所";
		$forms['save_date']="別途定める";
		$forms['dateestimated']="発行日より1ヶ月";
		$forms['period_remark']="次回改定まで";
		
		$smarty->assign('forms', $forms);
		$smarty->assign('consumer', $row1);
		$smarty->assign('price_no', $price_no);
		$smarty->assign('staff_name', $staff_name);
		$smarty->assign('flag', 1);
		$smarty->assign('tax', $tax);
		$smarty->assign('num', 1);
		$HTTP_REFERER = $_SERVER['HTTP_REFERER'];
		$_SESSION['HTTP_REFERER'] = $HTTP_REFERER;
	
	}
	else if($mod =='delete'){
		$price_no = $_GET['price_no'];
		$consumer_id = $_GET['consumer_id'];
		$db = new Class_DB;
		$db->query("delete from p_consumer_head where price_no ='$price_no'");
		$db->query("delete from p_consumer_detail where price_no ='$price_no'");
		$db->close();
		$smarty->assign('phase', 'completeupdate');
		$smarty->assign("consumer_id",$consumer_id);
		
	}
	else if($mod == 'request'){
		$price_no = $_GET['price_no'];
		$consumer_id = $_GET['consumer_id'];
		$db = new Class_DB;
		$db->query("UPDATE p_consumer_head set status = 2 where price_no ='$price_no'");
		$smarty->assign('phase', 'completerequest');
		$smarty->assign("consumer_id",$consumer_id);
	}
	
	else if($mod == 'quoteprint'){
		$price_no = $_GET['price_no'];
		$consumer_id = $_GET['consumer_id'];
		$db = new Class_DB;
		$db->query("UPDATE p_consumer_head set quoteprint = 1 where price_no ='$price_no'");
		$smarty->assign('phase', 'completeupdate');
		$smarty->assign("consumer_id",$consumer_id);
	}
	
	else if($mod == 'revokequoteprint'){
		$price_no = $_GET['price_no'];
		$consumer_id = $_GET['consumer_id'];
		$db = new Class_DB;
		$db->query("UPDATE p_consumer_head set quoteprint = 0 where price_no ='$price_no'");
		$smarty->assign('phase', 'completeupdate');
		$smarty->assign("consumer_id",$consumer_id);
	}
	
	else if($mod == 'yes'){
		$date = date('Y/m/d G:i:s');
		$price_no = $_GET['price_no'];
		$consumer_id = $_GET['consumer_id'];
		$db = new Class_DB;
		$db->query("UPDATE p_consumer_head set status = 5 ,ok_date='$date' where price_no ='$price_no'");
		$smarty->assign('phase', 'completeyes');
		$smarty->assign("consumer_id",$consumer_id);
	}
	
	else if($mod == 'no'){
		$price_no = $_GET['price_no'];
		$consumer_id = $_GET['consumer_id'];
		$db = new Class_DB;
		$db->query("UPDATE p_consumer_head set status = 6 where price_no ='$price_no'");
		$smarty->assign('phase', 'completeupdate');
		$smarty->assign("consumer_id",$consumer_id);
	}
	
	else if($mod == 'copy'){
		
		$db = new Class_DB;
		
		$date = date('Ymd');
		$strSql = "SELECT * FROM p_consumer_head WHERE  price_no like '%".$date."%' order by price_no desc limit 0,1";
		$result = $db->query($strSql);
		$row = mysql_fetch_array($result);
		if(!empty($row['price_no'])){
			$strPrice_no = substr($row['price_no'],strlen($row['price_no'])-3,strlen($row['price_no']))+1;
			if(strlen($strPrice_no) == 1){
				$row['price_no'] = $date."00".$strPrice_no;
			}elseif (strlen($strPrice_no) == 2){
				$row['price_no'] = $date."0".$strPrice_no;
			}else{
				$row['price_no'] = $date.$strPrice_no;
			}
				
		}else{
			$row['price_no'] = $date."001";
		}
		
		$new_price_no = $row['price_no'];
		
		$price_no = $_GET['price_no'];
		$consumer_id = $_GET['consumer_id'];
		
		$sql1 = "SELECT * FROM p_consumer_head WHERE price_no ='$price_no' AND consumer_id=$consumer_id ";
		$sql2 = "SELECT * FROM p_consumer WHERE consumer_id=$consumer_id";
		$sql3 = "SELECT * FROM p_consumer_detail  WHERE price_no='$price_no'";
		$sql4 = "SELECT * FROM M_STAFF WHERE staff_id=".$staff_id;
		
		$result1 = $db->query($sql1);
		$result2 = $db->query($sql2);
		$result3 = $db->query($sql3);
		$result4 = $db->query($sql4);
		
		$row1 = mysql_fetch_array($result1);
		$row1['period_str']=$row1['period_str']==NULL?"":date('Y/m/d',strtotime($row1['period_str']));
		$row1['period_end']=$row1['period_end']==NULL?"":date('Y/m/d',strtotime($row1['period_end']));
		$row1['price_no'] = $new_price_no;
		
		$row2 = mysql_fetch_array($result2);
		$row3 = array();
		while( $row = mysql_fetch_array($result3,MYSQL_ASSOC) ){
			$row['money'] =number_format(intval($row['refer_price']*$row['discount']*$row['quantity']));
			$row['m'] =(intval($row['refer_price']*$row['discount']*$row['quantity']));
			$row['profit'] = intval((($row['m']-$row['cost_price']*$row['quantity'])/$row['m'])*100);
			$row['refer_price'] = intval($row['refer_price']);
			$row3[] = $row;
		}
		
		$row4 = mysql_fetch_array($result4);
		
		$db->close();
		$price_no = $row1['price_no'];
		$staff_name = $row1['producer'];
		$smarty->assign('forms', $row1);
		$smarty->assign('consumer', $row2);
		$smarty->assign('price_no', $price_no);
		$smarty->assign('staff_name', $row4['staff_name']);
		$smarty->assign('detail', $row3);
		$smarty->assign('flag', 1);
		$smarty->assign('num', count($row3)+1);
		
		
	}
	
	else{
		$db = new Class_DB;
		$price_no = $_GET['price_no'];
		$consumer_id = $_GET['consumer_id'];
		$sql1 = "SELECT * FROM p_consumer_head WHERE price_no ='$price_no' AND consumer_id=$consumer_id ";
		$sql2 = "SELECT * FROM p_consumer WHERE consumer_id=$consumer_id";
		$sql3 = "SELECT * FROM p_consumer_detail  WHERE price_no='$price_no'";
		$result1 = $db->query($sql1);
		$result2 = $db->query($sql2);
		$result3 = $db->query($sql3);
		
		$row1 = mysql_fetch_array($result1);
		$row1['period_str']=$row1['period_str']==NULL?"":date('Y/m/d',strtotime($row1['period_str']));
		$row1['period_end']=$row1['period_end']==NULL?"":date('Y/m/d',strtotime($row1['period_end']));
		if($row1['period_str']<='2000')$row1['period_str']="";
		if($row1['period_end']<='2000')$row1['period_end']="";
		
		$row2 = mysql_fetch_array($result2);
		$row3 = array();
		while( $row = mysql_fetch_array($result3,MYSQL_ASSOC) ){
			$row['money'] =number_format(intval($row['refer_price']*$row['discount']*$row['quantity']));
			$row['m'] =(intval($row['refer_price']*$row['discount']*$row['quantity']));
			$row['profit'] = intval((($row['m']-$row['cost_price']*$row['quantity'])/$row['m'])*100);
			$row['refer_price'] = intval($row['refer_price']);
			$row3[] = $row;
			
		
		}
		
		$db->close();
		$price_no = $row1['price_no'];
		$staff_name = $row1['producer'];
		$smarty->assign('forms', $row1);
		$smarty->assign('consumer', $row2);
		$smarty->assign('price_no', $price_no);
		$smarty->assign('staff_name', $staff_name);
		$smarty->assign('consumer_id', $consumer_id);
		$smarty->assign('detail', $row3);
		$smarty->assign('flag', 0);
		$smarty->assign('num', count($row3)+1);
		$HTTP_REFERER = $_SERVER['HTTP_REFERER'];
		$_SESSION['HTTP_REFERER'] = $HTTP_REFERER;
	}
	
	
}else{
	$mod = $_POST['mod'];
	$err = new Class_ERROR;
	if($_POST['payment'] == 2 ){
		$err_mes['payment_term'] = $err->check( $_POST['payment_term'], array("EXIST") );
	}
	$err_mes['save_address'] = $err->check( $_POST['save_address'], array("EXIST") );
	$err_mes['save_date'] = $err->check( $_POST['save_date'], array("EXIST") );
	$err_mes['dateestimated'] = $err->check( $_POST['dateestimated'], array("EXIST") );
	if( trim($_POST['period_remark']) == '' ){
		$err_mes['period'] = $err->check( $_POST['period_str'], array("EXIST") );
		$err_mes['period'] = $err->check( $_POST['period_end'], array("EXIST") );
	}
	
	if( $err->clear ){
		//head
		$price_no 		= $_POST['price_no'];
		$consumer_id 	= $_POST['consumer_id'];
		$producer		= $_POST['producer'];
		$payment		= $_POST['payment'];
		$payment_term	= $_POST['payment_term'];
		$pro_name		= $_POST['pro_name'];
		$save_address	= $_POST['save_address'];
		$save_date		= $_POST['save_date'];
		$period_remark	= $_POST['period_remark'];
		$period_str		= $_POST['period_str'];
		$period_end		= $_POST['period_end'];
		$dateestimated	= $_POST['dateestimated'];
		$shipping		= $_POST['shipping'];
		$remark			= $_POST['remark'];
		$special_reason	= $_POST['special_reason'];
		$staff_id		= $_SESSION['ADMIN_LOGIN']['STAFF_ID'];
		$date			= date('Y/m/d G:i:s');
		
		$mod = $_POST['mod'];
		if($mod == 'update'){
			
			$head_sql = "UPDATE p_consumer_head SET payment_term ='$payment_term' ,pro_name = '$pro_name',
			save_address = '$save_address',save_date = '$save_date',period_remark = '$period_remark',period_str = '$period_str',
			period_end = '$period_end',dateestimated = '$dateestimated',
			shipping = '$shipping',remark = '$remark',special_reason = '$special_reason',u_date ='$date',tax=$tax  WHERE price_no = '$price_no' ";
			$db = new Class_DB;
			$db ->query($head_sql);
			
			
			//detail
			$shop_ids		= $_POST['shop_id'];
			$multiples		= $_POST['multiple'];
			$nums			= $_POST['num'];
			$dids			= $_POST['did'];
			$refer_prices	= $_POST['refer_price'];
			$cost_prices	= $_POST['cost_price'];
			$shop_names		= $_POST['shop_name'];
			$s_units		= $_POST['s_unit'];
			$styles			= $_POST['style'];
			
			$detail_sql_value		= "";
			for($i=0;$i<count($shop_ids);$i++){
				if($shop_ids[$i]>0 && $dids[$i]==0){
					$detail_sql_value ="insert into p_consumer_detail values (null,'$price_no',$shop_ids[$i],$multiples[$i],$nums[$i],$refer_prices[$i],$cost_prices[$i],'$shop_names[$i]','$s_units[$i]','$styles[$i]')";
					$db ->query($detail_sql_value);
				}
				if($dids[$i] > 0){
					$update_detail_sql = "update p_consumer_detail set shop_id=$shop_ids[$i],discount=$multiples[$i],quantity=$nums[$i],refer_price=$refer_prices[$i],cost_price=$cost_prices[$i],shop_name='$shop_names[$i]',s_unit='$s_units[$i]',style='$styles[$i]' where did =$dids[$i] ";
					$db ->query($update_detail_sql);
				}
			}
			$db->close();
			$smarty->assign('phase', 'completeupdate');
			
		}
		else if($mod =='copy'){
			
			$db = new Class_DB;
			
			$date = date('Ymd');
			$strSql = "SELECT * FROM p_consumer_head WHERE  price_no like '%".$date."%' order by price_no desc limit 0,1";
			$result = $db->query($strSql);
			$row = mysql_fetch_array($result);
			if(!empty($row['price_no'])){
				$strPrice_no = substr($row['price_no'],strlen($row['price_no'])-3,strlen($row['price_no']))+1;
				if(strlen($strPrice_no) == 1){
					$row['price_no'] = $date."00".$strPrice_no;
				}elseif (strlen($strPrice_no) == 2){
					$row['price_no'] = $date."0".$strPrice_no;
				}else{
					$row['price_no'] = $date.$strPrice_no;
				}
					
			}else{
				$row['price_no'] = $date."001";
			}
			
			$price_no = $row['price_no'];
			$date			= date('Y/m/d G:i:s');
			
			$head_sql		= "INSERT INTO p_consumer_head (price_no,consumer_id,producer,payment_term,pro_name,
			save_address,save_date,period_remark,period_str,period_end,dateestimated,
			shipping,remark,special_reason,staff_id,u_date,m_date,status,tax) values
			('$price_no',$consumer_id,'$producer','$payment_term','$pro_name','$save_address','$save_date',
			'$period_remark','$period_str','$period_end','$dateestimated','$shipping','$remark','$special_reason',
			$staff_id,'$date','$date',1,$tax)";
			
			$db ->query($head_sql);
			
			//detail
			$shop_ids		= $_POST['shop_id'];
			$multiples		= $_POST['multiple'];
			$nums			= $_POST['num'];
			$refer_prices	= $_POST['refer_price'];
			$cost_prices	= $_POST['cost_price'];
			$shop_names		= $_POST['shop_name'];
			$s_units		= $_POST['s_unit'];
			$styles			= $_POST['style'];
			
			$detail_sql_value		= "";
			for($i=0;$i<count($shop_ids);$i++){
				if($shop_ids[$i]>0){
					$detail_sql_value .="(null,'$price_no',$shop_ids[$i],$multiples[$i],$nums[$i],$refer_prices[$i],$cost_prices[$i],'$shop_names[$i]','$s_units[$i]','$styles[$i]'),";
				}
			}
			$detail_sql_value = substr($detail_sql_value, 0,strlen($detail_sql_value)-1);
			//echo $detail_sql_value;
			if(strlen($detail_sql_value)>0){
				$db ->query("insert into p_consumer_detail values $detail_sql_value");
			}
			$db ->close();
			$smarty->assign('phase', 'completecopy');
			
		}
		else{
			
			$status = 1;
			if($mod == "yes") $status = 2;
			$head_sql		= "INSERT INTO p_consumer_head (price_no,consumer_id,producer,payment_term,pro_name,
			save_address,save_date,period_remark,period_str,period_end,dateestimated,
			shipping,remark,special_reason,staff_id,u_date,m_date,status,tax) values
			('$price_no',$consumer_id,'$producer','$payment_term','$pro_name','$save_address','$save_date',
			'$period_remark','$period_str','$period_end','$dateestimated','$shipping','$remark','$special_reason',
			$staff_id,'$date','$date',$status,$tax)";
			//echo $head_sql;
			$db = new Class_DB;
			$db ->query($head_sql);
				
			//detail
			$shop_ids		= $_POST['shop_id'];
			$multiples		= $_POST['multiple'];
			$nums			= $_POST['num'];
			$refer_prices	= $_POST['refer_price'];
			$cost_prices	= $_POST['cost_price'];
			$shop_names		= $_POST['shop_name'];
			$s_units		= $_POST['s_unit'];
			$styles			= $_POST['style'];
			
			$detail_sql_value		= "";
			for($i=0;$i<count($shop_ids);$i++){
				if($shop_ids[$i]>0){
					$detail_sql_value .="(null,'$price_no',$shop_ids[$i],$multiples[$i],$nums[$i],$refer_prices[$i],$cost_prices[$i],'$shop_names[$i]','$s_units[$i]','$styles[$i]'),";
				}
			}
			$detail_sql_value = substr($detail_sql_value, 0,strlen($detail_sql_value)-1);
			//echo $detail_sql_value;
			if(strlen($detail_sql_value)>0){
				$db ->query("insert into p_consumer_detail values $detail_sql_value");
			}
			$db ->close();
			$smarty->assign('phase', 'complete');
			
		}
		$smarty->assign("consumer_id",$consumer_id);
		
	}else{
		$smarty->assign('err', $err_mes);
		$smarty->assign('price_no', $_POST['price_no']);
		$smarty->assign('staff_name', $_POST['producer']);
		$smarty->assign('forms', $_POST);
		
		$db = new Class_DB;
		if($mod == 'update'){
			
			$price_no = $_POST['price_no'];
			$consumer_id = $_POST['consumer_id'];
			$sql1 = "SELECT * FROM p_consumer_head WHERE price_no ='$price_no' AND consumer_id=$consumer_id ";
			$sql2 = "SELECT * FROM p_consumer WHERE consumer_id=$consumer_id";
			$sql3 = "SELECT * FROM p_consumer_detail  WHERE price_no='$price_no'";
			$result1 = $db->query($sql1);
			$result2 = $db->query($sql2);
			$result3 = $db->query($sql3);
			
			$row1 = mysql_fetch_array($result1);
			$row1['period_str']=$row1['period_str']==NULL?"":date('Y/m/d',strtotime($row1['period_str']));
			$row1['period_end']=$row1['period_end']==NULL?"":date('Y/m/d',strtotime($row1['period_end']));
			
			
			$row2 = mysql_fetch_array($result2);
			$row3 = array();
			while( $row = mysql_fetch_array($result3,MYSQL_ASSOC) ){
				$row['money'] =number_format(intval($row['refer_price']*$row['discount']*$row['quantity']));
				$row['m'] =(intval($row['refer_price']*$row['discount']*$row['quantity']));
				$row['profit'] = intval((($row['m']-$row['cost_price']*$row['quantity'])/$row['m'])*100);
				$row['refer_price'] = intval($row['refer_price']);
				$row3[] = $row;
			}
			
			$db->close();
			
			//detail
			$shop_ids		= $_POST['shop_id'];
			$multiples		= $_POST['multiple'];
			$nums			= $_POST['num'];
			$dids			= $_POST['did'];
			$refer_prices	= $_POST['refer_price'];
			$cost_prices	= $_POST['cost_price'];
			$shop_names		= $_POST['shop_name'];
			$s_units		= $_POST['s_unit'];
			$styles			= $_POST['style'];
				
			$detail_sql_value		= "";
			for($i=0;$i<count($shop_ids);$i++){
				if($shop_ids[$i]>0 && $dids[$i]==0){
					$new_detail = array("shop_id"=>$shop_ids[$i],"discount"=>$multiples[$i],"quantity"=>$nums[$i],"did"=>$dids[$i],"refer_price"=>$refer_prices[$i],"cost_price"=>$cost_prices[$i],"shop_name"=>$shop_names[$i],"s_unit"=>$s_units[$i],"style"=>$styles[$i]);
					$new_detail['money'] =number_format(intval($new_detail['refer_price']*$new_detail['discount']*$new_detail['quantity']));
					$new_detail['m'] =(intval($new_detail['refer_price']*$new_detail['discount']*$new_detail['quantity']));
					$new_detail['profit'] = intval((($new_detail['m']-$new_detail['cost_price']*$new_detail['quantity'])/$new_detail['m'])*100);
					$new_detail['refer_price'] = intval($new_detail['refer_price']);
					$row3[] = $new_detail;
				}
				
			}
			
			$price_no = $row1['price_no'];
			$staff_name = $row1['producer'];
			$smarty->assign('consumer', $row2);
			$smarty->assign('price_no', $price_no);
			$smarty->assign('staff_name', $staff_name);
			$smarty->assign('detail', $row3);
			$smarty->assign('flag', 0);
			$smarty->assign('num', count($row3)+1);
			
		}else{
			
			$strSql1 = "SELECT * FROM p_consumer where consumer_id=".$_POST['consumer_id'];
			$result1 = $db->query($strSql1);
			$row1 = mysql_fetch_array($result1);
			$smarty->assign('consumer', $row1);
			$smarty->assign('flag', 1);
			
			//detail
			$shop_ids		= $_POST['shop_id'];
			$multiples		= $_POST['multiple'];
			$nums			= $_POST['num'];
			$dids			= $_POST['did'];
			$refer_prices	= $_POST['refer_price'];
			$cost_prices	= $_POST['cost_price'];
			$shop_names		= $_POST['shop_name'];
			$s_units		= $_POST['s_unit'];
			$styles			= $_POST['style'];
			
			$detail_sql_value		= "";
			for($i=0;$i<count($shop_ids);$i++){
				if($shop_ids[$i]>0 && $dids[$i]==0){
					$new_detail = array("shop_id"=>$shop_ids[$i],"discount"=>$multiples[$i],"quantity"=>$nums[$i],"did"=>$dids[$i],"refer_price"=>$refer_prices[$i],"cost_price"=>$cost_prices[$i],"shop_name"=>$shop_names[$i],"s_unit"=>$s_units[$i],"style"=>$styles[$i]);
					$new_detail['money'] =number_format(intval($new_detail['refer_price']*$new_detail['discount']*$new_detail['quantity']));
					$new_detail['m'] =(intval($new_detail['refer_price']*$new_detail['discount']*$new_detail['quantity']));
					$new_detail['profit'] = intval((($new_detail['m']-$new_detail['cost_price']*$new_detail['quantity'])/$new_detail['m'])*100);
					$new_detail['refer_price'] = intval($new_detail['refer_price']);
					$row3[] = $new_detail;
				}
			
			}
			
			$smarty->assign('detail', $row3);
			$smarty->assign('num', count($row3)+1);
		}
		
		
		
		
	}
	
	
}
	
$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );
$smarty->assign('menu_html', $menu_html);
$smarty->assign('tax', $tax);
$smarty->assign('pre_page_url', $_SERVER["HTTP_REFERER"]);
$smarty->display('web-admin/make_price.html');


?>